import routeConfig from './config';
import { Switch, Route, Redirect } from 'react-router-dom';

import PAGE404 from '@@/views/PAGE404';

/**
 * 处理子路由渲染
 * @param route 原路由配置对象
 * @returns 要渲染组件
 */
export const RouteWithSubRoutes = (route: any) => {
  return (
    <Route
      path={route.path}
      key={route.path}
      render={props => (
        // pass the sub-routes down to keep nesting
        <route.component {...props} routes={route.routes} />
      )}
    />
  )
}

const router = () => (
  <Switch>
    <Redirect path="/" to={routeConfig[0].path} exact={true}/>
    {
      routeConfig.map((route, i) => {
        if (route.routes) {
          return <RouteWithSubRoutes key={i} { ...route } />
        } else {
          return <Route key={i} {...route } />
        }
      })
    }
    <Route component={PAGE404}/>
  </Switch>
)

export default router;